<template>
  <div class="nice-buy">
    <h1>为您找到的优质货源哦</h1>
    <div class="title">
      <div class="title-left">易物用品</div>
    </div>
    <div class="nice-buy-box">
      <div
        class="nine-buy-item"
        v-for="(item, index) in searchList"
        :key="item.id"
      >
        <el-image :src="'https://a2.ngrok.kjjvip.com' + item.img">
          <div slot="placeholder" class="image-slot">
            加载中<span class="dot">...</span>
          </div>
        </el-image>
        <div class="content">
          <div class="content-title">
            {{ item.title }}
          </div>
          <span class="price">{{ item.condition }}</span>
          <div class="gobuy">
            <div class="price">
              <span>{{ item.desc }}</span
              ><span></span>
            </div>
            <span>{{ item.user.name }}</span>
            <div class="buy-btn">
              <el-button
                type="warning"
                size="small"
                @click="
                  goinfo(
                    item.id,
                    item.title,
                    item.desc,
                    item.user.name,
                    item.user.id,
                    item.condition,
                    item.img
                  )
                "
                >查看详情</el-button
              >
            </div>
          </div>
        </div>
      </div>
    </div>
    <p v-if="searchList.length == 0">当时暂无任何商品哦~~</p>
  </div>
</template>

<script>
import { getSearch } from "network/request.js";
export default {
  data() {
    return {
      loading: true,
      searchList: [],
      title: this.$route.query.title,
    };
  },

  created() {
    this.getsearchList();
  },
  mounted() {
    this.$nextTick(() => {
      // this.initScroll()
      window.onscroll = () => {
        //变量scrollTop是滚动条滚动时，距离顶部的距离
        let scrollTop =
          document.documentElement.scrollTop || document.body.scrollTop;
        //变量windowHeight是可视区的高度
        let windowHeight =
          document.documentElement.clientHeight || document.body.clientHeight;
        //变量scrollHeight是滚动条的总高度
        let scrollHeight =
          document.documentElement.scrollHeight || document.body.scrollHeight;
        //滚动条到底部的条件
        if (scrollTop + windowHeight == scrollHeight) {
          //写后台加载数据的函数
          // console.log("距顶部"+scrollTop+"可视区高度"+windowHeight+"滚动条总高度"+scrollHeight);
          if (this.loading) this.getsearchList();
        }
      };
    });
  },
  methods: {
    async getsearchList() {
      const { data: res } = await getSearch({ params: { title: this.title } });
      console.log(res);
      this.searchList = res.content.data;
    },

    goinfo(gid, title, desc, sname, sid, condition, img) {
      this.$router.push({
        path: "/Goods",
        query: {
          gid: gid,
          title: title,
          desc: desc,
          sname: sname,
          sid: sid,
          condition: condition,
          simg: img,
        },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.nice-buy {
  padding-top: 100px;
  width: 90%;
  margin: 0 auto;
  h1 {
    text-align: center;
    &::before {
      content: "---";
      color: #ccc;
      padding: 0px 5px;
    }
    &::after {
      content: "---";
      color: #ccc;
      padding: 0px 5px;
    }
  }

  .title {
    padding: 10px 0px;
    .title-left {
      font-size: 20px;
      padding-left: 10px;
      border-bottom: 2px solid #000;
      border-left: 5px solid #f8b81e;
    }
  }

  .nice-buy-box {
    width: 100%;
    margin: 0 auto;
    justify-content: center;
    display: flex;
    flex-wrap: wrap;
    .nine-buy-item {
      position: relative;
      text-align: center;
      .cover {
        text-align: center;
        background-color: #eee;
        // top: 0;
        z-index: 999;
        opacity: 0.6;
        width: 100%;
        height: 100%;
        position: absolute;
        img {
          margin-top: 50%;
        }
      }
      &:hover {
        transition: 0.1 all;
        transform: scale3d(1.01, 1.01, 1);
        cursor: pointer;
      }
      margin: 3%;
      width: 20%;
      .el-image {
        width: 80%;
      }

      .content {
        .content-title {
          padding: 20px 10px;
        }
        .price {
          color: #f8b81e;
        }
        .gobuy {
          display: flex;
          margin: 10px 20px;
          justify-content: space-between;
          .price {
            span:nth-child(1) {
              font-size: 20px;
              color: #f8b81e;
            }
          }

          .buy-btn {
            el-button {
            }
          }
        }
      }
    }
  }

  .getmore {
    text-align: center;
    padding: 10px 0px;
    color: #ddd;
    &:hover {
      cursor: pointer;
      background-color: #eee;
    }
  }
}
</style>